package com.young.common.query;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/28/24 4:42 PM
 */
public class PaginationBuilder<T> implements Serializable {
    private Pagination<T> pagination;
    private volatile boolean completed = false;

    protected PaginationBuilder(Query query) {
        this.pagination = new Pagination(query);
    }

    public static <T> PaginationBuilder<T> query(Query input) {
        return new PaginationBuilder(input);
    }

    public PaginationBuilder<T> result(Collection<T> result) {
        if (this.completed) {
            return this;
        } else {
            this.pagination.result = result;
            return this;
        }
    }

    public PaginationBuilder<T> amount(int amount) {
        if (this.completed) {
            return this;
        } else {
            this.pagination.amount = amount;
            return this;
        }
    }

    public Pagination<T> build() {
        if (this.completed) {
            return this.pagination;
        } else {
            this.pagination.complete();
            this.completed = true;
            return this.pagination;
        }
    }

    public Pagination<T> reference(String key, Object value) {
        if (this.completed) {
            return this.pagination;
        } else {
            if (this.pagination.reference == null) {
                this.pagination.reference = new HashMap();
            }

            this.pagination.reference.put(key, value);
            return this.pagination;
        }
    }

    public Pagination<T> reference(Map<String, ?> mapper) {
        if (this.completed) {
            return this.pagination;
        } else {
            if (this.pagination.reference == null) {
                this.pagination.reference = new HashMap();
            }

            this.pagination.reference.putAll(mapper);
            return this.pagination;
        }
    }
}

